//
//  AOStatusCell.swift
//  weibo_zha
//
//  Created by _zha on 16/10/27.
//  Copyright © 2016年 渣. All rights reserved.
//

import UIKit

class AOStatusCell: UITableViewCell {
    
    // 底部视图顶部约束记录
    var bottomViewTopCons: Constraint?
    
    //
    var statusViewModel: AOStatusViewModel? {
        didSet{
            //给原创微博赋值
            originalView.statusViewModel = statusViewModel
            
//            let picUrls = statusViewModel?.statusModel?.pic_urls
//            print(picUrls)
            // 卸载约束
            bottomViewTopCons?.uninstall()
            //没有转发微博
            if statusViewModel?.statusModel?.retweeted_status == nil {
                bottomView.snp_updateConstraints(closure: { (make) in
                    self.bottomViewTopCons = make.top.equalTo(originalView.snp_bottom).constraint
                })
                
                // 隐藏
                retweetView.isHidden = true
                
            }else {
                // 切记先赋值
                retweetView.statusViewModel = statusViewModel
                
                // 有转发微博
                bottomView.snp_updateConstraints(closure: { (make) in
                    self.bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                // 显示
                retweetView.isHidden = false
            }
            // 底部视图赋值
            bottomView.statusViewModel = statusViewModel
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        //
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        //
        originalView.snp_makeConstraints { (make) in
            make.top.equalTo(contentView).offset(8)
            make.left.right.equalTo(contentView)
        }
        retweetView.snp_makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
//            make.height.equalTo(50)
        }
        bottomView.snp_makeConstraints { (make) in
            self.bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
            make.left.right.equalTo(contentView)
            make.height.equalTo(35)
            make.bottom.equalTo(contentView)
        }
    }
    

    //懒加载控件
    private lazy var originalView: AOStatusOriginalView = AOStatusOriginalView()
    // 转发微博
    private lazy var retweetView: AOStatusRetweetView = AOStatusRetweetView()
    // 底部视图
    private lazy var bottomView: AOStatusBottomView = AOStatusBottomView()
}
